package com.level2app.ui.login;

import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.Nullable;

import com.level2app.R;
import com.level2app.base.BaseActivity;
import com.level2app.config.CoreManager;
import com.level2app.databinding.ActivityCheckEmailBinding;
import com.level2app.ui.app.MainActivity;
import com.level2app.ui.login.view.CheckEmailViewModel;
import com.level2app.ui.welcome.FinishSignUpActivity;
import com.level2app.utils.JumpIntent;
import com.level2app.utils.ToastHelper;
import com.level2app.view.dialog.ChooseEmailDialog;

import java.util.List;

public class CheckEmailActivity extends BaseActivity<ActivityCheckEmailBinding, CheckEmailViewModel> {

    private int source = -1;//0--登录，1--注册
    private String email = "";

    @Override
    protected CheckEmailViewModel bindModel() {
        return getViewModel(this, CheckEmailViewModel.class);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handleLink();
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        initTitleBar();
        source = getIntent().getIntExtra("source", 0);
        email = getIntent().getStringExtra("email");
        binding.resendEmailTv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        binding.resendEmailTv.getPaint().setAntiAlias(true);
        binding.contentTv.setText(String.format(getString(R.string.we_ve_sent_), email));
        binding.resendEmailTv.startCountDown(this, 30, R.string.resend_email, R.string.resend_email_un_click);
        binding.resendEmailTv.setDisabledStrokeColorRes(R.color.color_9ca3af);
        binding.resendEmailTv.setNormalStrokeColorRes(R.color.color_141416);
    }

    @Override
    protected void initClick() {
        super.initClick();
        viewModel.onDelayClick(binding.resendEmailTv, o -> {
            showLoadingDialog();
            if (source == 0) {
                viewModel.sendEmailLogin(email);
            } else {
                //注册
                viewModel.sendEmailRegister(email);
            }
        });
        viewModel.onDelayClick(binding.openEmailBtn, o -> {
            ChooseEmailDialog chooseEmailDialog = new ChooseEmailDialog(context, type -> {
                if (type.equals("gmail")) {
                    Uri uri = Uri.parse("https://gmail.app.goo.gl");
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                } else {
                    Uri uri = Uri.parse("mailto:");
                    Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
                    startActivity(intent);
                }
            });
            chooseEmailDialog.show();
        });
        viewModel.sendLoginSuccessData.observe(this, messageBean -> {
            dismissLoadingDialog();
            ToastHelper.showToastAndImage(messageBean.getMessage(), R.mipmap.icon_toast_error);
            binding.resendEmailTv.startCountDown(this, 30, R.string.resend_email, R.string.resend_email_un_click);
        });
        viewModel.mErrorData.observe(this, string -> {
            dismissLoadingDialog();
        });
    }

    private void handleLink() {
        //APP link
        // ATTENTION: This was auto-generated to handle app links.
        Intent appLinkIntent = getIntent();
        if (appLinkIntent == null) return;
        String appLinkAction = appLinkIntent.getAction();
        Uri appLinkData = appLinkIntent.getData();

        if (appLinkData != null) {
            // 获取URL中的参数等
            appLinkData.getScheme();//获取scheme
            appLinkData.getHost();//获取host
            appLinkData.getAuthority();//获取authority
            Log.e("===", "uri=" + appLinkData);
            String token = appLinkData.getQueryParameter("access");
            String refresh = appLinkData.getQueryParameter("refresh");
            if (!TextUtils.isEmpty(token)) {
                CoreManager.setToken(token);
                if (!TextUtils.isEmpty(refresh)) {
                    CoreManager.setRefresh(refresh);
                }
                //魔法链接验证成功后
                if (source == 1) {
                    //注册
                    JumpIntent.jump(this, FinishSignUpActivity.class);
                } else if (source == 0) {
                    //登录
                    JumpIntent.jumpToMainFinishAll(this);
                } else {
                    boolean isLogin = CoreManager.getIsLogin();
                    if (isLogin) {
                        source = 0;
                        JumpIntent.jumpToMainFinishAll(this);
                    } else {
                        source = 1;
                        JumpIntent.jump(this, FinishSignUpActivity.class);
                    }
                }
            }
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        handleLink();
    }
}